package ${domain};

import ${domain}.frame.auth.Token;
import ${domain}.frame.base.BaseResponse;
import ${domain}.frame.utils.MapperUtil;
import ${domain}.module.wsys.ent.Res;
import ${domain}.module.wsys.mgr.DictItemManager;
import ${domain}.module.wsys.mgr.DictManager;
import ${domain}.module.wsys.mgr.ResManager;
import ${domain}.module.wsys.mgr.RoleManager;
import ${domain}.module.wsys.mgr.RoleResManager;
import ${domain}.module.wsys.req.DictCreateRequest;
import ${domain}.module.wsys.req.DictFindRequest;
import ${domain}.module.wsys.req.DictItemCreateRequest;
import ${domain}.module.wsys.req.DictItemFindRequest;
import ${domain}.module.wsys.req.ResCreateRequest;
import ${domain}.module.wsys.req.ResFindRequest;
import ${domain}.module.wsys.req.RoleCreateRequest;
import ${domain}.module.wsys.req.RoleFindRequest;
import ${domain}.module.wsys.req.RoleResCheckRequest;
import ${domain}.module.wsys.rsp.DictCreateResponse;
import ${domain}.module.wsys.rsp.DictFindResponse;
import ${domain}.module.wsys.rsp.DictItemCreateResponse;
import ${domain}.module.wsys.rsp.DictItemFindResponse;
import ${domain}.module.wsys.rsp.ResCreateResponse;
import ${domain}.module.wsys.rsp.ResFindResponse;
import ${domain}.module.wsys.rsp.RoleCreateResponse;
import ${domain}.module.wsys.rsp.RoleFindResponse;
import ${domain}.module.wsys.rsp.RoleResCheckResponse;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.junit.Assert.assertTrue;

@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class DataInit {

    @Autowired
    private Token token;
    @Autowired
    private DictManager dictManager;
    @Autowired
    private DictItemManager dictItemManager;
    @Autowired
    private ResManager resManager;
    @Autowired
    private RoleManager roleManager;
    @Autowired
    private RoleResManager roleCreateRequest;

    /**
     * 字典数据初始化
     */
    @Test
    @Rollback(false)
    public void init() {
        //region 字典初始化
        {
<#list project.modules as module>
    <#list module.tables as table>
        <#list table.fields as field>
            <#if field.fieldType == 'Dict' && module.name != 'wsys'>
            {// ${field.fieldComment?default('')}
                createDict("${field.fieldName?default('')}", "${field.fieldComment?default('')}", new String[][]{
                    <#list field.selectItems as item>
                        {"${item.value?default('')}", "${item.label?default('')}"},
                    </#list>
                });
            }
            </#if>
        </#list>
    </#list>
</#list>
        }
        //endregion

        //region 管理员初始化
        {
            List<Res> forBase = new ArrayList<>();
            {// 基础 资源
                forBase.add(createRes("00", "管理系统", "WEB", "/|/index.htm"));
                forBase.add(createRes("0000", "基础资源", "WEB", "PUB"));
                forBase.add(createRes("000001", "首页", "WEB", "/home.htm"));
                forBase.add(createRes("00000101", "总数数据统计", "WEB", "/ajax/wsys/data/total"));
                forBase.add(createRes("00000102", "登录数据统计", "WEB", "/ajax/wsys/data/login"));
                forBase.add(createRes("00000103", "任务数据统计", "WEB", "/ajax/wsys/data/task"));
                forBase.add(createRes("00000104", "故障数据统计", "WEB", "/ajax/wsys/data/error"));
                forBase.add(createRes("000002", "开发指导页", "WEB", "/helpDev.htm"));
                forBase.add(createRes("000003", "使用指导页", "WEB", "/help.htm"));
            }

            List<Res> forDev = new ArrayList<>();
            {// 初始化开发角色特有资源
                forDev.add(createRes("0001", "核心设置菜单", "WEB", "DEV"));
                forDev.add(createRes("000101", "系统配置菜单", "WEB", "DEV_PROFILES"));
                forDev.add(createRes("00010101", "系统配置管理页", "WEB", "/wsys/profiles.htm"));
                forDev.add(createRes("0001010101", "系统配置模板", "WEB", "/ajax/wsys/profiles/template"));
                forDev.add(createRes("0001010102", "系统配置导入", "WEB", "/ajax/wsys/profiles/imports"));
                forDev.add(createRes("0001010103", "系统配置导出", "WEB", "/ajax/wsys/profiles/exports"));
                forDev.add(createRes("0001010104", "系统配置新增", "WEB", "/ajax/wsys/profiles/create"));
                forDev.add(createRes("0001010105", "系统配置删除", "WEB", "/ajax/wsys/profiles/delete"));
                forDev.add(createRes("0001010106", "系统配置编辑", "WEB", "/ajax/wsys/profiles/update"));
                forDev.add(createRes("0001010107", "系统配置查询", "WEB", "/ajax/wsys/profiles/find"));
                forDev.add(createRes("0001010108", "系统配置获取", "WEB", "/ajax/wsys/profiles/get"));
                forDev.add(createRes("000102", "资源管理菜单", "WEB", "DEV_RES"));
                forDev.add(createRes("00010201", "资源管理页", "WEB", "/wsys/res.htm"));
                forDev.add(createRes("0001020101", "资源模板", "WEB", "/ajax/wsys/res/template"));
                forDev.add(createRes("0001020102", "资源导入", "WEB", "/ajax/wsys/res/imports"));
                forDev.add(createRes("0001020103", "资源导出", "WEB", "/ajax/wsys/res/exports"));
                forDev.add(createRes("0001020104", "资源新增", "WEB", "/ajax/wsys/res/create"));
                forDev.add(createRes("0001020105", "资源删除", "WEB", "/ajax/wsys/res/delete"));
                forDev.add(createRes("0001020106", "资源编辑", "WEB", "/ajax/wsys/res/update"));
                forDev.add(createRes("0001020107", "资源查询", "WEB", "/ajax/wsys/res/find"));
                forDev.add(createRes("0001020108", "资源获取", "WEB", "/ajax/wsys/res/get"));
                forDev.add(createRes("000103", "角色管理菜单", "WEB", "DEV_ROLE"));
                forDev.add(createRes("00010301", "角色管理页", "WEB", "/wsys/role.htm"));
                forDev.add(createRes("0001030101", "角色模板", "WEB", "/ajax/wsys/role/template"));
                forDev.add(createRes("0001030102", "角色导入", "WEB", "/ajax/wsys/role/imports"));
                forDev.add(createRes("0001030103", "角色导出", "WEB", "/ajax/wsys/role/exports"));
                forDev.add(createRes("0001030104", "角色新增", "WEB", "/ajax/wsys/role/create"));
                forDev.add(createRes("0001030105", "角色删除", "WEB", "/ajax/wsys/role/delete"));
                forDev.add(createRes("0001030106", "角色更新", "WEB", "/ajax/wsys/role/update"));
                forDev.add(createRes("0001030107", "角色查询", "WEB", "/ajax/wsys/role/find"));
                forDev.add(createRes("0001030108", "角色获取", "WEB", "/ajax/wsys/role/get"));
                forDev.add(createRes("0001030109", "角色加载", "WEB", "/ajax/wsys/role/load"));
                forDev.add(createRes("0001030110", "角色资源分配", "WEB", "/ajax/wsys/roleRes/check"));
                forDev.add(createRes("0001030111", "角色资源关系查询", "WEB", "/ajax/wsys/roleRes/find"));
                forDev.add(createRes("0001030112", "资源树查询", "WEB", "/ajax/wsys/res/tree"));
                forDev.add(createRes("000104", "SQL任务菜单", "WEB", "DEV_TASK_SQL"));
                forDev.add(createRes("00010401", "SQL任务管理页", "WEB", "/wsys/taskSql.htm"));
                forDev.add(createRes("0001040101", "SQL任务模板", "WEB", "/ajax/wsys/taskSql/template"));
                forDev.add(createRes("0001040102", "SQL任务导入", "WEB", "/ajax/wsys/taskSql/imports"));
                forDev.add(createRes("0001040103", "SQL任务导出", "WEB", "/ajax/wsys/taskSql/exports"));
                forDev.add(createRes("0001040104", "SQL任务新增", "WEB", "/ajax/wsys/taskSql/create"));
                forDev.add(createRes("0001040105", "SQL任务删除", "WEB", "/ajax/wsys/taskSql/delete"));
                forDev.add(createRes("0001040106", "SQL任务编辑", "WEB", "/ajax/wsys/taskSql/update"));
                forDev.add(createRes("0001040107", "SQL任务查询", "WEB", "/ajax/wsys/taskSql/find"));
                forDev.add(createRes("0001040108", "SQL任务获取", "WEB", "/ajax/wsys/taskSql/get"));
                forDev.add(createRes("000105", "应用接入管理菜单", "WEB", "DEV_VISITOR"));
                forDev.add(createRes("00010501", "应用接入管理页", "WEB", "/wsys/visitor.htm"));
                forDev.add(createRes("0001050101", "应用接入模板", "WEB", "/ajax/wsys/visitor/template"));
                forDev.add(createRes("0001050102", "应用接入导入", "WEB", "/ajax/wsys/visitor/imports"));
                forDev.add(createRes("0001050103", "应用接入导出", "WEB", "/ajax/wsys/visitor/exports"));
                forDev.add(createRes("0001050104", "应用接入新增", "WEB", "/ajax/wsys/visitor/create"));
                forDev.add(createRes("0001050105", "应用接入删除", "WEB", "/ajax/wsys/visitor/delete"));
                forDev.add(createRes("0001050106", "应用接入更新", "WEB", "/ajax/wsys/visitor/update"));
                forDev.add(createRes("0001050107", "应用接入查询", "WEB", "/ajax/wsys/visitor/find"));
                forDev.add(createRes("0001050108", "应用资源关系新增", "WEB", "/ajax/wsys/visitorRes/create"));
                forDev.add(createRes("0001050109", "应用资源关系删除", "WEB", "/ajax/wsys/visitorRes/delete"));
                forDev.add(createRes("0001050110", "应用资源关系更新", "WEB", "/ajax/wsys/visitorRes/update"));
                forDev.add(createRes("0001050111", "应用资源关系查询", "WEB", "/ajax/wsys/visitorRes/find"));

                forDev.add(createRes("0002", "监控运行菜单", "WEB", "MNT"));
                forDev.add(createRes("000201", "登录管理菜单", "WEB", "MNT_TOKENS"));
                forDev.add(createRes("00020101", "登录管理页", "WEB", "/wsys/tokens.htm"));
                forDev.add(createRes("0002010101", "登录查询", "WEB", "/ajax/wsys/tokens/find"));
                forDev.add(createRes("0002010102", "登录导出", "WEB", "/ajax/wsys/tokens/exports"));
                forDev.add(createRes("0002010103", "登录注销", "WEB", "/ajax/wsys/tokens/logout"));
                forDev.add(createRes("000202", "调度监控菜单", "WEB", "MNT_SCHEDULE"));
                forDev.add(createRes("00020201", "调度监控页", "WEB", "/wsys/schedule.htm"));
                forDev.add(createRes("0002020101", "调度监控查询", "WEB", "/ajax/wsys/schedule/find"));
                forDev.add(createRes("0002020102", "调度监控启动", "WEB", "/ajax/wsys/schedule/start"));
                forDev.add(createRes("0002020103", "调度监控暂停", "WEB", "/ajax/wsys/schedule/stop"));
                forDev.add(createRes("000203", "任务日志管理菜单", "WEB", "MNT_LOGTASK"));
                forDev.add(createRes("00020301", "任务日志管理页", "WEB", "/wsys/logtask.htm"));
                forDev.add(createRes("0002030101", "任务日志模板", "WEB", "/ajax/wsys/logtask/template"));
                forDev.add(createRes("0002030102", "任务日志导入", "WEB", "/ajax/wsys/logtask/imports"));
                forDev.add(createRes("0002030103", "任务日志导出", "WEB", "/ajax/wsys/logtask/exports"));
                forDev.add(createRes("0002030104", "任务日志新增", "WEB", "/ajax/wsys/logtask/create"));
                forDev.add(createRes("0002030105", "任务日志删除", "WEB", "/ajax/wsys/logtask/delete"));
                forDev.add(createRes("0002030106", "任务日志更新", "WEB", "/ajax/wsys/logtask/update"));
                forDev.add(createRes("0002030107", "任务日志查询", "WEB", "/ajax/wsys/logtask/find"));
                forDev.add(createRes("000204", "故障日志菜单", "WEB", "MNT_LOGERR"));
                forDev.add(createRes("00020401", "故障日志管理页", "WEB", "/wsys/logerr.htm"));
                forDev.add(createRes("0002040101", "故障日志删除", "WEB", "/ajax/wsys/logerr/delete"));
                forDev.add(createRes("0002040102", "故障日志更新", "WEB", "/ajax/wsys/logerr/update"));
                forDev.add(createRes("0002040103", "故障日志查询", "WEB", "/ajax/wsys/logerr/find"));
            }

            List<Res> forMgr = new ArrayList<>();
            {// 初始化管理资源
                forMgr.add(createRes("0003", "基础设置菜单", "WEB", "SYS"));

                forMgr.add(createRes("000302", "字典管理菜单", "WEB", "SYS_DICT"));
                forMgr.add(createRes("00030201", "字典管理页", "WEB", "/wsys/dict.htm"));
                forMgr.add(createRes("0003020101", "字典模板", "WEB", "/ajax/wsys/dict/template"));
                forMgr.add(createRes("0003020102", "字典导入", "WEB", "/ajax/wsys/dict/imports"));
                forMgr.add(createRes("0003020103", "字典导出", "WEB", "/ajax/wsys/dict/exports"));
                forMgr.add(createRes("0003020104", "字典新建", "WEB", "/ajax/wsys/dict/create"));
                forMgr.add(createRes("0003020105", "字典删除", "WEB", "/ajax/wsys/dict/delete"));
                forMgr.add(createRes("0003020106", "字典更新", "WEB", "/ajax/wsys/dict/update"));
                forMgr.add(createRes("0003020107", "字典查询", "WEB", "/ajax/wsys/dict/find"));
                forMgr.add(createRes("0003020108", "字典获取", "WEB", "/ajax/wsys/dict/get"));
                forMgr.add(createRes("0003020109", "字典详情", "WEB", "/ajax/wsys/dict/load", true));
                forMgr.add(createRes("0003020110", "字典项管理页", "WEB", "/wsys/dictItem.htm"));
                forMgr.add(createRes("000302011001", "字典项模板", "WEB", "/ajax/wsys/dictItem/template"));
                forMgr.add(createRes("000302011002", "字典项导入", "WEB", "/ajax/wsys/dictItem/imports"));
                forMgr.add(createRes("000302011003", "字典项导出", "WEB", "/ajax/wsys/dictItem/exports"));
                forMgr.add(createRes("000302011004", "字典项新增", "WEB", "/ajax/wsys/dictItem/create"));
                forMgr.add(createRes("000302011005", "字典项删除", "WEB", "/ajax/wsys/dictItem/delete"));
                forMgr.add(createRes("000302011006", "字典项更新", "WEB", "/ajax/wsys/dictItem/update"));
                forMgr.add(createRes("000302011007", "字典项查找", "WEB", "/ajax/wsys/dictItem/find"));
                forMgr.add(createRes("000302011008", "字典项获取", "WEB", "/ajax/wsys/dictItem/get"));

                forMgr.add(createRes("000303", "用户管理菜单", "WEB", "SYS_USER"));
                forMgr.add(createRes("00030301", "用户管理页", "WEB", "/wsys/user.htm"));
                forMgr.add(createRes("0003030101", "字用户模板", "WEB", "/ajax/wsys/user/template"));
                forMgr.add(createRes("0003030102", "用户导入", "WEB", "/ajax/wsys/user/imports"));
                forMgr.add(createRes("0003030103", "用户导出", "WEB", "/ajax/wsys/user/exports"));
                forMgr.add(createRes("0003030104", "用户新增", "WEB", "/ajax/wsys/user/create"));
                forMgr.add(createRes("0003030105", "用户删除", "WEB", "/ajax/wsys/user/delete"));
                forMgr.add(createRes("0003030106", "用户更新", "WEB", "/ajax/wsys/user/update"));
                forMgr.add(createRes("0003030107", "用户查询", "WEB", "/ajax/wsys/user/find"));
                forMgr.add(createRes("0003030108", "用户获取", "WEB", "/ajax/wsys/user/get"));
                forMgr.add(createRes("0003030109", "修改密码", "WEB", "/ajax/wsys/user/changePwd", true));
                forMgr.add(createRes("0003030110", "重置密码", "WEB", "/ajax/wsys/user/resetPwd"));

                forMgr.add(createRes("000304", "机构管理菜单", "WEB", "SYS_DEPT"));
                forMgr.add(createRes("00030401", "机构管理页", "WEB", "/wsys/dept.htm"));
                forMgr.add(createRes("0003040101", "机构新增", "WEB", "/ajax/wsys/dept/create"));
                forMgr.add(createRes("0003040102", "机构删除", "WEB", "/ajax/wsys/dept/delete"));
                forMgr.add(createRes("0003040103", "机构更新", "WEB", "/ajax/wsys/dept/update"));
                forMgr.add(createRes("0003040104", "机构查询", "WEB", "/ajax/wsys/dept/find"));
                forMgr.add(createRes("0003040105", "机构获取", "WEB", "/ajax/wsys/dept/get"));
                forMgr.add(createRes("0003040106", "机构树查询", "WEB", "/ajax/wsys/dept/tree"));
                forMgr.add(createRes("0003040107", "机构模板", "WEB", "/ajax/wsys/dept/template"));
                forMgr.add(createRes("0003040108", "机构导入", "WEB", "/ajax/wsys/dept/imports"));
                forMgr.add(createRes("0003040109", "机构导出", "WEB", "/ajax/wsys/dept/exports"));

                forMgr.add(createRes("000305", "文件管理菜单", "WEB", "SYS_FILE"));
                forMgr.add(createRes("00030501", "文件管理页", "WEB", "/wsys/file.htm"));
                forMgr.add(createRes("0003050101", "文件删除", "WEB", "/ajax/wsys/file/delete"));
                forMgr.add(createRes("0003050102", "文件查询", "WEB", "/ajax/wsys/file/find"));

<#list project.modules as module>
<#assign index = module_index + 4/>
<#if index<10>
<#assign index = "0"+index/>
</#if>
<#if module.getNeedGenerate()>
                forMgr.add(createRes("00${index}", "${module.note}菜单", "WEB", "${module.name?upper_case}"));
    <#list module.tables as table>
        <#if table.isPage()>
                forMgr.add(createRes("00${index}01", "${table.tableComment}管理菜单", "WEB", "${module.name?upper_case}_${table.tableName?upper_case}"));
                forMgr.add(createRes("00${index}0101", "${table.tableComment}管理页", "WEB", "/${module.name}/${table.getFName()}.htm"));
                forMgr.add(createRes("00${index}010101", "${table.tableComment}模板", "WEB", "/ajax/${module.name}/${table.getFName()}/template"));
                forMgr.add(createRes("00${index}010102", "${table.tableComment}导入", "WEB", "/ajax/${module.name}/${table.getFName()}/imports"));
                forMgr.add(createRes("00${index}010103", "${table.tableComment}导出", "WEB", "/ajax/${module.name}/${table.getFName()}/exports"));
        </#if>
        <#if table.create>
                forMgr.add(createRes("00${index}010104", "${table.tableComment}新增", "WEB", "/ajax/${module.name}/${table.getFName()}/create"));
        </#if>
        <#if table.delete>
                forMgr.add(createRes("00${index}010105", "${table.tableComment}删除", "WEB", "/ajax/${module.name}/${table.getFName()}/delete"));
        </#if>
        <#if table.update>
                forMgr.add(createRes("00${index}010106", "${table.tableComment}更新", "WEB", "/ajax/${module.name}/${table.getFName()}/update"));
        </#if>
        <#if table.find>
                forMgr.add(createRes("00${index}010107", "${table.tableComment}查询", "WEB", "/ajax/${module.name}/${table.getFName()}/find"));
        </#if>
        <#if table.get>
                forMgr.add(createRes("00${index}010108", "${table.tableComment}获取", "WEB", "/ajax/${module.name}/${table.getFName()}/get"));
        </#if>
        <#if table.search>
                forMgr.add(createRes("00${index}010109", "${table.tableComment}搜索", "WEB", "/ajax/${module.name}/${table.getFName()}/search"));
        </#if>
    </#list>
</#if>
</#list>
            }
<#if project.isNeedApi()>

            {// API资源
                forMgr.add(createRes("01", "接口资源", "API", "A"));
                forMgr.add(createRes("0101", "基础资源", "API", "A01"));
                forMgr.add(createRes("010101", "用户登录", "API", "/api/wsys/User/login"));
                forMgr.add(createRes("010102", "字典查询", "API", "/api/wsys/Dict/find"));
                forMgr.add(createRes("010103", "加载字典", "API", "/api/wsys/Dict/load"));
                forMgr.add(createRes("010104", "文件上传", "API", "/api/wsys/File/upload"));
                forMgr.add(createRes("010105", "文件下载", "API", "/api/wsys/File/download"));
<#list project.modules as module>
<#assign index = module_index + 2/>
<#if index<10>
<#assign index = "0"+index/>
</#if>
                forMgr.add(createRes("01${index}", "${module.note}", "API", "A${module.name?upper_case}"));
    <#list module.tables as table>
                forMgr.add(createRes("01${index}01", "${table.tableComment}", "API", "A${module.name?upper_case}_${table.tableName?upper_case}"));
        <#if table.create>
                forMgr.add(createRes("01${index}0101", "${table.tableComment}新增", "API", "/api/${module.name}/${table.getFName()}/create"));
        </#if>
        <#if table.delete>
                forMgr.add(createRes("01${index}0102", "${table.tableComment}删除", "API", "/api/${module.name}/${table.getFName()}/delete"));
        </#if>
        <#if table.update>
                forMgr.add(createRes("01${index}0103", "${table.tableComment}更新", "API", "/api/${module.name}/${table.getFName()}/update"));
        </#if>
        <#if table.find>
                forMgr.add(createRes("01${index}0104", "${table.tableComment}查询", "API", "/api/${module.name}/${table.getFName()}/find"));
        </#if>
        <#if table.get>
                forMgr.add(createRes("01${index}0105", "${table.tableComment}获取", "API", "/api/${module.name}/${table.getFName()}/get"));
        </#if>
        <#if table.search>
                forMgr.add(createRes("01${index}0107", "${table.tableComment}搜索", "API", "/api/${module.name}/${table.getFName()}/search"));
        </#if>
    </#list>
</#list>
            }
</#if>

            {//初始化开发角色
                Long roleId = createRole("developer", "开发", "开发");
                for (Res res : forBase) {
                    allocateRes(roleId, "developer", res.getId(), res.getResCode());
                }
                for (Res res : forDev) {
                    allocateRes(roleId, "developer", res.getId(), res.getResCode());
                }
                for (Res res : forMgr) {
                    allocateRes(roleId, "developer", res.getId(), res.getResCode());
                }
            }
            {//初始化管理角色
                Long roleId = createRole("management", "管理员", "管理员");
                for (Res res : forBase) {
                    allocateRes(roleId, "management", res.getId(), res.getResCode());
                }
                for (Res res : forMgr) {
                    allocateRes(roleId, "management", res.getId(), res.getResCode());
                }
            }
        }
        //endregion
    }

    private void createDict(String name, String comment, String[][] items) {
        DictFindRequest dictFindRequest = new DictFindRequest();
        dictFindRequest.setDictName(name);
        DictFindResponse dictFindResponse = dictManager.find(dictFindRequest, token);
        if (dictFindResponse.getResult().size() == 0) {
            DictCreateRequest dictCreateRequest = new DictCreateRequest();
            dictCreateRequest.setDictName(name);
            dictCreateRequest.setDictNote(comment);
            dictCreateRequest.setVersion(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            dictCreateRequest.setValid(true);
            DictCreateResponse dictCreateResponse = dictManager.create(dictCreateRequest, token);
            printError(dictCreateResponse);
            assertTrue(!dictCreateResponse.hasError());
        }

        // 通过查询获取字典项已有数量
        DictItemFindRequest dictItemFindRequest = new DictItemFindRequest();
        dictItemFindRequest.setDictName(name);
        DictItemFindResponse dictItemFindResponse = dictItemManager.find(dictItemFindRequest, token);

        for (int i = 0; i < items.length; i++) {
            String[] item = items[i];
            dictItemFindRequest.setKey(item[0]);
            DictItemFindResponse dictItemFindResponse_ = dictItemManager.find(dictItemFindRequest, token);
            if (dictItemFindResponse_.getResult().size() == 0) {
                DictItemCreateRequest dictItemCreateRequest = new DictItemCreateRequest();
                dictItemCreateRequest.setDictName(name);
                dictItemCreateRequest.setKey(item[0]);
                dictItemCreateRequest.setValue(item[1]);
                dictItemCreateRequest.setSort((int) (i + dictItemFindResponse.getTotalCount()));
                dictItemCreateRequest.setValid(true);
                DictItemCreateResponse dictItemCreateResponse = dictItemManager.create(dictItemCreateRequest, token);
                printError(dictItemCreateResponse);
                assertTrue(!dictItemCreateResponse.hasError());
            }
        }
    }

    private Res createRes(String code, String name, String resType, String value) {
        return createRes(code, name, resType, value, false);
    }

    private Res createRes(String code, String name, String resType, String value, boolean free) {
        ResFindRequest resFindRequest = new ResFindRequest();
        resFindRequest.setResCode(code);
        ResFindResponse resFindResponse = resManager.find(resFindRequest, token);
        if (resFindResponse.getResult().size() == 0) {
            ResCreateRequest resCreateRequest = new ResCreateRequest();
            resCreateRequest.setResName(name);
            resCreateRequest.setResCode(code);
            resCreateRequest.setResType(resType);
            resCreateRequest.setResValue(value);
            resCreateRequest.setFree(free);
            resCreateRequest.setValid(true);
            ResCreateResponse resCreateResponse = resManager.create(resCreateRequest, token);
            printError(resCreateResponse);
            assertTrue(!resCreateResponse.hasError());

            Res res = new Res();
            res.setResCode(code);
            res.setResName(name);
            res.setResType(resType);
            res.setId(resCreateResponse.getId());
            return res;
        } else {
            System.err.println("已经存在的资源:" + value);
            return resFindResponse.getResult().get(0);
        }
    }

    private Long createRole(String roleCode, String roleName, String roleComment) {
        RoleFindRequest roleFindRequest = new RoleFindRequest();
        roleFindRequest.setCode(roleCode);
        RoleFindResponse roleFindResponse = roleManager.find(roleFindRequest, token);
        if (roleFindResponse.getResult().size() == 0) {
            RoleCreateRequest roleCreateRequest = new RoleCreateRequest();
            roleCreateRequest.setCode(roleCode);
            roleCreateRequest.setName(roleName);
            roleCreateRequest.setComment(roleComment);
            RoleCreateResponse roleCreateResponse = roleManager.create(roleCreateRequest, token);
            printError(roleCreateResponse);
            assertTrue(!roleCreateResponse.hasError());
            return roleCreateResponse.getId();
        } else {
            return roleFindResponse.getResult().get(0).getId();
        }
    }

    private void allocateRes(Long roleId, String roleCode, Long resId, String resCode) {
        RoleResCheckRequest roleResCheckRequest = new RoleResCheckRequest();
        roleResCheckRequest.setRoleId(roleId);
        roleResCheckRequest.setRoleCode(roleCode);
        roleResCheckRequest.setResId(resId);
        roleResCheckRequest.setResCode(resCode);
        roleResCheckRequest.setChecked(true);

        RoleResCheckResponse roleResCheckResponse = roleCreateRequest.check(roleResCheckRequest, token);
        printError(roleResCheckResponse);
        assertTrue(!roleResCheckResponse.hasError());
    }

    private void printError(BaseResponse response) {
        if (response.hasError()) {
            System.err.println("==========>" + MapperUtil.toJson(response));
        }
    }
}